package org.sxf.app.sign.utils;



/**
 * Created by siasun on 16/2/18.
 */
public class CosUtils {

    public static String intToHexString(int input, int length){
        String arg = "%0" + length*2 + "x";
        return String.format(arg, input);
    }

    public static String deCode(String s, String key){
        byte[] deCode = PBOCDES.pboc3DESDecrypt(PBOCDES.parseBytes(key), PBOCDES.parseBytes(s));
        byte[] len = new byte[]{deCode[0], deCode[1]};
        int lenght = Integer.parseInt(Utility.bytesToHexString(len),16);
        byte[] value = new byte[lenght];
        System.arraycopy(deCode, 2, value, 0, lenght);
        String data = new String(value);
        return data;
    }

    public static String cosSub(String inputStr, int startPos, int endPos){
        if (inputStr == null){
            return "";
        }
        if (startPos == 0){
            startPos = 1;
        }
        if (endPos > inputStr.length()){
            endPos = inputStr.length();
        }
        startPos -= 1;
        return inputStr.substring(startPos * 2, endPos * 2);
    }

    public static String cosReplace(String inputStr, String replaceStr, int startPos, int endPos){
        if (inputStr == null){
            return "";
        }
        if (replaceStr == null){
            return "";
        }
        if (startPos == 0){
            startPos = 1;
        }
        if ((endPos - startPos + 1)*2 != replaceStr.length()){
//            LogUtils.e("CosReplace endPos:" + endPos + ",startPos:" + startPos + ",replaceStr.length:" + replaceStr.length());
            endPos = replaceStr.length()/2 + startPos - 1;
        }
        if (endPos > inputStr.length()){
            endPos = inputStr.length();
        }
        startPos -= 1;
        StringBuffer buffer = new StringBuffer(inputStr);

        return buffer.replace(startPos*2, endPos*2, replaceStr).toString();
    }

    public static String record2TradeType(String rsp){
        String result = "";
        try {
            String tradeTypeStr = cosSub(rsp, 10, 10);
            if (tradeTypeStr.equals("06")){
                result = "消费";
            }else if (tradeTypeStr.equals("09")){
                result = "消费";
            }else if (tradeTypeStr.equals("02")){
                result = "充值";
            }
        }catch (Exception e){
            return result;
        }
        return result;
    }

    public static String record2TradeDate(String rsp){
        String result = "";
        try {
            result = cosSub(rsp, 17, 18) + "-" + cosSub(rsp, 19, 19) + "-" + cosSub(rsp, 20, 20);
        }catch (Exception e){
            return result;
        }
        return result;
    }

    public static String record2TradeTime(String rsp){
        String result = "";
        try {
            result = cosSub(rsp, 21, 21) + ":" + cosSub(rsp, 22, 22) + ":" + cosSub(rsp, 23, 23);
        }catch (Exception e){
            return result;
        }
        return result;
    }


    public static String formatCardNo(String cardNo){
        String result = cardNo;
        try {
            result = cosSub(cardNo, 1, 2) + "-" + cosSub(cardNo, 3, 4) + "-" + cosSub(cardNo, 5, 6) + "-" + cosSub(cardNo, 7, 8);
        }catch (Exception e){
            return result;
        }
        return result;
    }

}
